package cn.yunhe.controller;

import cn.yunhe.entity.Exam;
import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.User;
import cn.yunhe.service.impl.ExamServiceImpl;
import cn.yunhe.util.DateUtil;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ExamController {

    @Resource
    private ExamServiceImpl examService;

    @RequestMapping("getExamByPage")
    public Map getExamByPage(int pageSize, int currPage, Exam exam, String exampaper, String username, String starttime, String endtime) {
        if (exampaper != null && !"".equals(exampaper)) {
            ExamPaper examPaper = new ExamPaper();
            examPaper.setName(exampaper);
            exam.setExamPaper(examPaper);
        }
        if (username != null && !"".equals(username)) {
            User user = new User();
            user.setUserName(username);
            exam.setUser(user);
        }
        try{
        if(starttime != null && !"".equals(starttime)){
            exam.setStartTime(DateUtil.String2DateTime(starttime));
        }
        if(endtime != null && !"".equals(endtime)){
            exam.setEndTime(DateUtil.String2DateTime(endtime));
        }}catch (Exception e){}
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("currPage", currPage);
        map.put("exam", exam);
        Page page = examService.getExamByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/ExamAddExam")
    public String addExam(HttpSession session, String examName, int examType, String examIntro, int ExamPaperId, String startTime, String endTime){
        //封装数据
        Exam exam = new Exam();
        exam.setExamName(examName);
        exam.setExamType((byte)examType);
        exam.setExamIntro(examIntro);
        exam.setApproved((byte)0);
        System.out.println(startTime);

        //封装时间
        try {
            System.out.println(startTime);
            Date sTime = DateUtil.String2DateTime(startTime);
            Date eTime = DateUtil.String2DateTime(endTime);
            Date cTime = new Date();
            exam.setCreateTime(cTime);
            exam.setStartTime(sTime);
            exam.setEndTime(eTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //封装用户
        User user= (User) session.getAttribute("Curr_User");
        exam.setUser(user);
        //封装试卷
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(ExamPaperId);
        exam.setExamPaper(examPaper);

        int flag = examService.addExam(exam);
        if (flag>0){
            //执行成功
            return "redirect:find-examPaper.html";
        }else{
            //执行失败
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg","考试发布失败,请联系管理员");
            return "abc.ftl";
        }
    }

    public ExamServiceImpl getExamService() {
        return examService;
    }

    public void setExamService(ExamServiceImpl examService) {
        this.examService = examService;
    }
}
